<template>
  <div>
    <!-- width使用百分比, max-width使用具体的xxxpx , 用以适配小屏幕的PC端 -->
    <el-dialog
      :title="title"
      v-model="visable"
      width="65%"
      top="15vh"
      :close-on-click-modal="false"
      :draggable="true"
      style="max-width: 850px;"
    >
      <div class="bs-dialog">
        <el-form
          ref="addRef"
          :model="dialogFormData"
          :rules="dialogFormRules"
          label-width="auto"
          style="width: 95%;padding-left: 5%;"
        >
        <el-row :gutter="25">
          <el-col :span="12">
            <el-form-item label="手机号:" prop="phone">
              <el-input v-model="dialogFormData.phone" placeholder="请输入手机号"  clearable :disabled="!isBlank(dialogFormData.id)"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录密码:" prop="loginPassword">
              <el-input v-model="dialogFormData.loginPassword" placeholder="请输入登录密码" type="password" show-password clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信号:" prop="wechatID">
              <el-input v-model="dialogFormData.wechatID" placeholder="请输入微信号"  clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户姓名:" prop="name">
              <el-input v-model="dialogFormData.name" placeholder="请输入名称"  clearable />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="微信昵称:" prop="wechatNickname">
              <el-input v-model="dialogFormData.wechatNickname" placeholder="请输入微信昵称"  clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自媒体平台: ">
              <el-select
                v-model="dialogFormData.weMediaPlatform"
                placeholder="自媒体平台" 
                
                clearable
              >
                <el-option
                  v-for="item in weMediaPlatformList"
                  :key="item.dicValue"
                  :label="item.dicName"
                  :value="item.dicValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="平台粉丝昵称:" prop="platformNickname">
              <el-input v-model="dialogFormData.platformNickname" placeholder="请输入平台粉丝昵称"  clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号:" prop="account">
              <template #label>
                <el-tooltip
                  class="box-item"
                  effect="dark"
                  content="保证金监控中心账号"
                  placement="bottom-start"
                >
                <span><el-icon style="font-size: 15px;vertical-align: sub;"><QuestionFilled /></el-icon>账号:</span>
                </el-tooltip>
              </template>
              <el-input v-model="dialogFormData.account" placeholder="请输入账号"  clearable ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码:" prop="password">
              <template #label>
                <el-tooltip
                  class="box-item"
                  effect="dark"
                  content="保证金监控中心密码"
                  placement="bottom-start"
                >
                <span><el-icon style="font-size: 15px;vertical-align: sub;"><QuestionFilled /></el-icon>密码:</span>
                </el-tooltip>
              </template>
              <el-input v-model="dialogFormData.password" placeholder="请输入密码" type="password" show-password clearable ></el-input>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="引流人:" prop="recommended">
              <el-input v-model="dialogFormData.recommended" placeholder="请输入引流人"  clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="引流时间:" prop="recommendedTime">
              <el-date-picker 
                v-model="dialogFormData.recommendedTime"
                type="datetime"
                placeholder="请选择引流时间"
              />
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="状态:" prop="status"  style="text-align: left;">
              <el-radio-group v-model="dialogFormData.status">
                <el-radio :value="0">禁用</el-radio>
                <el-radio :value="1">正常</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->
        </el-row>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer" style="text-align: right;margin-right: 3%;">
          <el-button  size="small" @click="visable = false"> 取 消 </el-button>
          <el-button  size="small" type="primary" @click="save"> 保 存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { webRegister, updateStudent, getByID } from '@/api/oss/student.js'
import { selectDictionaryByTypeCode } from '@/api/system/dictionary.js'
// (修改和新增共用一个弹窗时,this.$refs['xxx'].resetFields()函数失效, 因此使用这种方式重置表单数据)
let orgDialogFormData = {
        id: undefined,
        phone:"",
        loginPassword:"",
        name:"",
        wechatID:"",
        status:1,
      }
export default {
  data() {
    const validatePhone = (rule, value, callback) => {
        if(value) {
            if (value.length != 11) {
                callback(new Error('请输入11位手机号'))
            } else {
                const reg = /^1[3456789]\d{9}$/;
                let flag = reg.test(value)
                if (flag == true) {
                    callback()
                } else {
                    callback(new Error('手机号码格式不正确'))
                }
            }
        } else {
            callback(new Error('手机号码不能为空'))
        }
    }
    return {
      title:"",
      visable: false,
      dialogFormData: { ...orgDialogFormData},
      dialogFormRules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
          { min: 11, max: 11, message: '手机号为11位字符', trigger: ['blur', 'change'] },
          { validator: validatePhone, trigger: ['blur', 'change'] }
        ],
        wechatID: [
          { required: true, message: '请输入微信号', trigger: ['blur', 'change'] },
          { validator: validatePhone, trigger: ['blur', 'change'] }
        ],
        loginPassword: [
          { required: true, message: '请输入登录', trigger: ['blur', 'change'] },
          { min: 6,  message: '登录密码最少为6位', trigger: ['blur', 'change'] },
        ],
      },
      // 自媒体平台字典列表
      weMediaPlatformList:[]
      
    }
  },
  methods: {
    openModal(id) {
      console.log("openModal=",id);
      this.visable = true
      this.$nextTick(() => {
        this.dialogFormData = {...orgDialogFormData}
        selectDictionaryByTypeCode({"dicTypeCode":"weMediaPlatform"}).then(res=>{
          if (res.code == 1) {
            this.weMediaPlatformList = res.data
          }
        })
        if (this.isNotBlank(id)) {
          this.title = '修改学员信息'
          // 获取修改的信息
          getByID({ 'id': id }).then(res => {
            console.log("修改返回值=",res);
            if (res.code === 1) {
              this.dialogFormData = {...res.data}
            }
          })
        } else {
          this.title = '新增学员信息'
        }
      })
    },
    
    // 保存
    save() {
      this.$refs['addRef'].validate((valid) => {
        if (valid) {
          if (this.isBlank(this.dialogFormData.id)) {
            webRegister(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                //this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          } else {
            updateStudent(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          }
          

        }
      })
    },

  }
}
</script>
<style scoped>
  /deep/.el-drawer__title {
    text-align: left;
  }
  :deep(.el-form-item__content) {
    display: inline-block;
    text-align: left;
    padding-right: 10%;
  }
  /deep/.el-drawer__title {
    text-align: left;
  }
  :deep(.el-input),
  :deep(.el-input-number--small) {
    width: 100%;
  }
</style>
